import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
import { Modal, message } from 'antd'
import { mouldList1, deleteMould1, newMould1, infoMould1, updateMould1, codeList1, newCode1, changeCode1 } from 'services/configManager'
import { userLis, searchs } from "services/search";
import { get } from 'http';

export default {
	namespace: 'configManager',
	state: {
		showMenu: '1',
		list: '',
		info: '',
		codeList:'',
		pagination1: {
			showSizeChanger: true,
			showQuickJumper: true,
			showTotal: total => `共 ${total} 条`,
			current: 1,
			total: null
		}
	},
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(location => {
				const { query, pathname } = location;
				if (pathname === "/sys/configManager") {
					dispatch({
						type: "queryList",
						payload: location.query
					});
				}
			});
		}
	},
	effects: {
		*info({ payload }, { call, put }) {
			const data = yield call(infoMould1, payload)
			if (data.success) {
				yield put({
					type: 'infoSuccess',
					payload: {
						info: data.template,
						showMenu: 3,
					}
				})
			}
		},
		*changeMenu({ payload }, { call, put }) {
			if (payload == 3) {
				yield put({
					type: 'showNewMenu1',
					payload: {
						showMenu: payload,
						info: ''
					}
				})
			} else {
				yield put({
					type: 'showMenu1',
					payload: {
						showMenu: payload
					}
				})
			}

		},
		*newFile({ payload }, { call, put }) {
			const data = yield call(newMould1, payload);
			if (data.success) {
				yield put({ type: 'queryList' })
				message.success('新增模板成功')
				yield put({
					type: 'addSuccess',
					payload: {
						showMenu: 1,
					}
				})
			}
			//			Modal.confirm({
			//		        title: '提示',
			//		        content: '操作成功',
			//		        okText: '确认',
			//		        cancelText: '取消',
			//		        onOk:function(){
			//		           window.location = '/sys/configManager'
			//		        }
			//		   });
		},
		*update({ payload }, { call, put }) {
			const data = yield call(updateMould1, payload);
			if (data.success) {
				yield put({ type: 'queryList' })
				message.success('修改增模板成功')
				yield put({
					type: 'addSuccess',
					payload: {
						showMenu: 1,
					}
				})
			}
			//			Modal.confirm({
			//		        title: '提示',
			//		        content: '操作成功',
			//		        okText: '确认',
			//		        cancelText: '取消',
			//		        onOk:function(){
			//		           window.location = '/sys/configManager'
			//		        }
			//		   });
		},
		*queryList({ payload }, { call, put }) {
			const data = yield call(mouldList1, payload);
			if (data.success) {
				yield put({
					type: 'querySuccess',
					payload: {
						list: data.page.list,
						pagination1: {
							total: data.page.totalCount,
							current: data.page.currPage
						}
					}
				})
			}
		},
		*reload(action, { put, select }) {
			const page = yield select(({ configManager }) => configManager.pagination1);
			yield put({
				type: "queryList",
				payload: {
					page: page.current,
				}
			});
		},
		*delete({ payload }, { call, put }) {
			const data = yield call(deleteMould1, payload);
			if (data.success) {
				yield put({
					type: 'reload'
				})
			}
		},
		*getCodeList({ payload },{ call, put }){
			const data = yield call(codeList1,payload)
			if(data){
				yield put({
					type:"getCodeSuccess",
					payload:{
						codeList:data.list
					}
				})
			}
		},
		*newCode({ payload },{ call, put }){
			const data = yield call(newCode1,payload)
		},
		*updateCode({ payload },{ call, put }){
			const data = yield call(changeCode1,payload)
		}
	},
	reducers: {
		querySuccess(state, action) {
			const { list, pagination1 } = action.payload;
			return {
				...state,
				list,
				pagination1: {
					...state.pagination1,
					...pagination1
				},
			}
		},
		showMenu1(state, action) {
			const { showMenu } = action.payload;
			return {
				...state,
				showMenu
			}
		},
		showNewMenu1(state, action) {
			const { showMenu, info } = action.payload;
			return {
				...state,
				showMenu,
				info
			}
		},
		infoSuccess(state, action) {
			const { info, showMenu } = action.payload;
			return {
				...state,
				info,
				showMenu
			}
		},
		addSuccess(state, action) {
			const { showMenu } = action.payload
			return {
				...state,
				showMenu
			}
		},
		showConfig(state, payload) {
			return {
				...state,
				showMenu: payload.payload
			}
		},
		getCodeSuccess(state,action){
			const { codeList } = action.payload
			return {
				...state,
				codeList
			} 
		}
	}
}
